package com.scalar.chiptrack.finance.creditNote.actionCommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.sales.dao.CustomerDAO;
import com.scalar.chiptrack.sales.dao.NewSalesOrderDAO;
import com.scalar.chiptrack.finance.creditNote.forms.CreditNoteForm;
import com.scalar.chiptrack.operations.packingslip.dao.NewPackingSlipDAO;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

/**
 * User: Sujan
 * Date: Apr 13, 2007
 * Time: 3:22:26 PM
 */
public class CreditNoteSearchNewCommand extends ActionCommand
{

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception 
    {
        HttpSession session  = request.getSession();
        User user            = ( User ) session.getAttribute ( SessionKeys.USER_KEY );
        CreditNoteForm creditNoteForm = (CreditNoteForm) form;
        request.setAttribute ( "customers", CustomerDAO.getAllCustomerNamesByUserType ( null, user ) );
        ArrayList salesOrders = NewSalesOrderDAO.loadShippedSalesOrderNumbersByCustomerId( null, creditNoteForm.getCustomerId() );
        request.setAttribute ( "salesOrderNumbers", salesOrders );
        ArrayList packingSlips = null;
        if ( creditNoteForm.getSalesOrderId() > 0 ) //sales order is selected
        {
            packingSlips = NewPackingSlipDAO.loadPackingSlipsBySalesOrderId( creditNoteForm.getSalesOrderId(), Constants.CUSTOMER_SHIPPING_STATUS_SHIPPED );
        }
        else
        {
            packingSlips = NewPackingSlipDAO.loadPackingSlipsByCustomerId( creditNoteForm.getCustomerId(), Constants.CUSTOMER_SHIPPING_STATUS_SHIPPED );    
        }
        request.setAttribute ( "packingSlipLabels", packingSlips );
        return mapping.findForward ( "searchNew" );
    }
}
